package com.zzyl.serve.service.impl;

import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.dto.NursingPlanDTO;
import com.zzyl.serve.domain.vo.NursingPlanVO;
import com.zzyl.serve.domain.vo.NursingProjectPlanVO;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY = "nursingPlan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id) {
        NursingPlanVO nursingPlanVO = nursingPlanMapper.selectNursingPlanById(id);
        if (nursingPlanVO != null) {
            List<NursingProjectPlanVO> nursingProjectPlans = nursingProjectPlanMapper.selectNursingProjectPlanListByPlanId(id);
            if (!CollectionUtils.isEmpty(nursingProjectPlans)) {
                nursingPlanVO.setProjectPlans(nursingProjectPlans);
            }
        }
        return nursingPlanVO;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO, nursingPlan);
        int i = nursingPlanMapper.insert(nursingPlan);
        if (!CollectionUtils.isEmpty(nursingPlanDTO.getProjectPlans())) {
            i = nursingProjectPlanMapper.batchInsert(nursingPlanDTO.getProjectPlans(), nursingPlan.getId());
        }
        if (i <= 0) {
            throw new ServiceException("保存护理计划失败");
        }
        deleteCache();
        return i;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO, nursingPlan);
        int i = nursingPlanMapper.updateById(nursingPlan);
        if (!CollectionUtils.isEmpty(nursingPlanDTO.getProjectPlans())) {
            // 先删除老的
            nursingProjectPlanMapper.delete(Wrappers.<NursingProjectPlan>lambdaQuery()
                    .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            // 再新增
            i = nursingProjectPlanMapper.batchInsert(nursingPlanDTO.getProjectPlans(), nursingPlan.getId());
        }
        if (i <= 0) {
            throw new ServiceException("保存护理计划失败");
        }
        deleteCache();
        return i;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
//        // 判断是否有项目关联计划
//        Long count = nursingProjectPlanMapper.selectCount(Wrappers.<NursingProjectPlan>lambdaQuery()
//                .eq(NursingProjectPlan::getPlanId, id));
//        if (count > 0) {
//            throw new ServiceException("删除护理计划失败，该护理计划下存在项目");
//        }
        int i = removeById(id) ? 1 : 0;
        if (i > 0) {
            deleteCache();
        }
        return i;
    }

    @Override
    public List<NursingPlan> findAll() {
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (CollectionUtils.isNotEmpty(nursingPlans)) {
            return nursingPlans;
        }
        nursingPlans = nursingPlanMapper.selectList(Wrappers.<NursingPlan>lambdaQuery()
                .eq(NursingPlan::getStatus, 1));
        redisTemplate.opsForValue().set(CACHE_KEY, nursingPlans);
        return nursingPlans;
    }
    /// 删除redis缓存
    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY);
    }
}
